package com.clps.cn.ActiviteDay05;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.List;
import java.util.Objects;

public class findGroupTaskList {

    @Test
    public void test001() {
        //01得到processEngin
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();

        //02得到ResitoryService对象
        RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
        //03部署
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("diagram/holiday05.bpmn")
                .name("请假流程-流程变量")
                .deploy();
        System.out.println("deploy.getId() = " + deploy.getId());
        System.out.println("deploy.getName() = " + deploy.getName());
    }


    @Test
    public void test02() {
        //01得到processEngin
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();

        //02得到runService对象
        RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();

        //03创建出实例相关的信息
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess_1");

        //04输出相关的信息
        System.out.println("流程定义id" + processInstance.getProcessDefinitionId());
        System.out.println("流程实例的id" + processInstance.getId());


    }


    @Test
    public void test003() {

        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();

        TaskService taskService = defaultProcessEngine.getTaskService();

        //03：根据流程定义的key得到任务表
        List<Task> list = taskService.createTaskQuery()
                .processDefinitionKey("myProcess_1")
                .taskAssignee("xiaozhang")
                .list();
        //任务列表
        for (Task task : list) {
            System.out.println("task.getProcessInstanceId() = " + task.getProcessInstanceId());
            System.out.println("task.getId() = " + task.getId());
            System.out.println("task.getAssignee() = " + task.getAssignee());
            System.out.println("task.getName() = " + task.getName());
            if (!Objects.isNull(task.getId())) {
                taskService.complete(task.getId());
            }
        }
    }

    //查询对应的任务
    @Test
    public void test04() {
        //01流程定义
        String processDefintionKey = "myProcess_1";
        //任务候选人
        String candidateUser = "zhangsan";
        //创建taskService
        //得到ProcessEngline的对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //得到runServicxe对象
        TaskService taskService = processEngine.getTaskService();
        //查询任务
        List<Task> list = taskService.createTaskQuery()
                .processDefinitionKey(processDefintionKey)
                .taskCandidateUser(candidateUser)
                .list();

        for (Task task : list) {
            System.out.println("------------------");
            System.out.println("task.getProcessInstanceId() = " + task.getProcessInstanceId());
            System.out.println("task.getId() = " + task.getId());
            System.out.println("task.getAssignee() = " + task.getAssignee());
            System.out.println("task.getName() = " + task.getName());

        }
    }

    /**
     * 拾取自己的任务
     */
    @Test
    public void test05() {
        //要拾取的任务
        String taskId = "5002";
        //任务候选人id
        String userId = "zhangsan";
        //得到ProcessEngline的对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //得到runServicxe对象
        TaskService taskService = processEngine.getTaskService();
        //要拾取自己的任务
        Task task = taskService.createTaskQuery()
                .taskId(taskId)
                .taskCandidateUser(userId)
                .singleResult();
        if (!Objects.isNull(task)) {
            taskService.claim(taskId, userId);
            System.out.println("任务拾取成功");
        }
    }


    //查询方式同一个任务查询
    @Test
    public void test06() {
        //流程定义key
        String processDefintionKey = "myProcess_1";
        //任务负责人
        String assignee = "zhangsan";

        //创建taskService
        //得到ProcessEngline的对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //创建taskService
        TaskService taskService = processEngine.getTaskService();
        List<Task> list = taskService.createTaskQuery()
                .processDefinitionKey(processDefintionKey)
                .taskAssignee(assignee).list();
        for (Task task : list) {
            System.out.println("---------------------------");
            System.out.println("流程实例id:" + task.getProcessInstanceId());
            System.out.println("任务id:" + task.getId());
            System.out.println("任务负责人:" + task.getAssignee());
            System.out.println("任务名称:" + task.getName());
        }

    }


    //执行任务
    @Test
    public void completeTask() {

        //任务id
        String taskId = "5002";
        //得到ProcessEngline的对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //任务id
        processEngine.getTaskService()
                .complete(taskId);
        System.out.println("完成任务 = " + taskId);
    }


    //归还任务,由个人任务变成组任务，还可以进行任务交接
    @Test
    public void setAssigneeToGroupTask() {
        //得到ProcessEngline的对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //创建taskService
        TaskService taskService = processEngine.getTaskService();
        //当前代办任务
        String taskId = "5002";
        //任务负责人
        String userId = "zhangsan";

        //校验yserId是否是负责人，如果是负责人可以归组内任务
        Task task = taskService.createTaskQuery().taskId(taskId)
                .taskAssignee(userId).singleResult();
        if (!Objects.isNull(task)) {
            taskService.setAssignee(taskId, null);
        }
    }


    //把zhangsan的任务转接给lisi
    @Test
    public void setAssigneeToCandidateUser() {
        //查询任务的使用taskService

        //得到ProcessEngline的对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //查询任务使用TaskService
        TaskService taskService = processEngine.getTaskService();
        //当前代办任务
        String taskId = "5002";
        //任务负责人
        String userId = "zhangsan";
        //将此任务交给其他候选人去办理任务
        String candidateuser = "lisi";

        //校验userId是否是负责人,如果是负责人才可以进去
        Task task = taskService.createTaskQuery().taskId(taskId)
                .taskAssignee(userId).singleResult();
        if(Objects.isNull(task)){
            taskService.setAssignee(taskId,candidateuser);
        }


    }

}
